package vip.xumy.core.utils;

import lombok.Getter;

public enum ImageType {
	PNG('P', 'N', 'G', '-', "png"), JPG('J', 'F', 'I', 'F', "jpg"), GIF('G', 'I', 'F', '-', "gif");
	@Getter
	private char begin, mid, end, last;
	@Getter
	private String type;

	private ImageType(char begin, char mid, char end, char last, String type) {
		this.begin = begin;
		this.mid = mid;
		this.end = end;
		this.last = last;
		this.type = type;
	}

	/**
	 * 获取图片类型
	 * 
	 * @param data
	 * @return
	 */
	public static ImageType getImageType(byte[] data) {
		// Png:
		if (PNG.getBegin() == data[1] && PNG.getMid() == data[2] && PNG.getEnd() == data[3]) {
			return PNG;
		}
		// Gif:
		if (GIF.getBegin() == data[0] && GIF.getMid() == data[1] && GIF.getEnd() == data[2]) {
			return GIF;
		}
		// JPG:
		if (JPG.getBegin() == data[6] && JPG.getMid() == data[7] && JPG.getEnd() == data[8]
				&& JPG.getLast() == data[9]) {
			return JPG;
		}
		return null;
	}
}